// pages/sale/orderList/orderList.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      flag:'',  //判断传过来的是什么类型
      active: 2, //是否激活 1未上传凭证，2已上传凭证 ,3未采购，4已采购
      // buyactive: 3, //是否激活 
      lists:[], //页面显示数据
      showTips: false,//没数据是否显示购买提示
      showType: 0,
      // active 1未激活，2激活，0没有激活该功能
      orderList:[],
      selectNum:0, //订单被选中的数量
      totalMoney: 0, //订单总计金额
      totalNum: 0,//订单总数量
  },
  //检验某个订单是否被选中
  selectItem: function (event) {
    console.log("进入选择---");
    let itemIndex = event.target.dataset.itemIndex;
    let that = this;
    let cartList = that.data.lists;
    let orderlist = that.data.orderList;
    let ctotal = that.data.selectNum;
    console.log('isCheck:' + cartList[itemIndex].checked);
    let isCheck=cartList[itemIndex].checked = !cartList[itemIndex].checked;
    orderlist[itemIndex].checked = !orderlist[itemIndex].checked;
    console.log('isCheck2:' +isCheck);
    if (isCheck) {
      ctotal += 1;
    } else {
      ctotal -= 1;
    }
    that.setData({
      selectNum: ctotal,
      lists: cartList,
      orderList: orderlist
    })
    console.log(that.data.lists)
  },
  //选中激活样式
  goActived: function(e){
    console.log(this.data.lists);
    let list=this.data.orderList;
    let ac = e.currentTarget.dataset.active;
    let array;
    if(ac==1){
      console.log('ac:'+ac)
      array = list.filter(function (obj, i) {
        console.log("picurls：" + obj.picUrls)
        return obj.picUrls == undefined;
      })
    } else if (ac == 2){
      console.log('ac:' + ac)
      array = list.filter(function (obj, i) {
        console.log("picurls：" + obj.picUrls)
        return obj.picUrls != undefined;
      })
    } else if (ac == 3) {
      console.log('ac:' + ac)
      array = list.filter(function (obj, i) {
        console.log("purchase：" + obj.purchase)
        return obj.purchase == 0;
      })
    }else{
      console.log('ac:' + ac)
      array = list.filter(function (obj, i) {
        console.log("purchase：" + obj.purchase)
        return obj.purchase== 1;
      })
    }
    let orderTotal=this.calOrderTotal(array);
    this.setData({
      active: ac,
      lists:array,
      selectNum:0,
      totalNum: orderTotal.totalNum,
      totalMoney: orderTotal.totalMoney
    })
  },
  //跳转到分类
  goIndex: function(e){
    wx.switchTab({
      url: '/pages/catalog/catalog',
    })
  },
  //跳转到详情页
  goOrderDetail: function(e){
    console.log("订单id：" + e.currentTarget.dataset.id)
    if (this.data.showType>=3){
      this.data.active =0
    }
      wx.navigateTo({
        url: '/pages/sale/orderDetail/orderDetail?id=' + e.currentTarget.dataset.id + '&showType=' + this.data.active+'&index='+this.data.showType,
      })
  },
  //计算订单总计
  calOrderTotal:function(arr){
    let totalMoney = 0;//订单总计金额
    let totalNum = 0;//订单总数量
    console.log("数量：" + arr.length + ":" + totalNum);
    arr.forEach(function (obj) {
      totalMoney = totalMoney + obj.orderPrice;
    });
    totalNum = arr.length;
    return {totalMoney:totalMoney,totalNum:totalNum};
  },
  //获取订单信息
  getOrderList() {
    let that = this;
    that.data.lists=[];
    let showType=wx.getStorageSync('index');
    util.request(api.OrderList, {
      showType: showType,
      status: 'sale'
    }).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        let arr = res.data.data;
        arr.forEach(function(obj){
          obj.checked=false;
        });
        console.log(arr)
        console.log(arr.length == 0)
        let showtip = arr.length == 0;//没数据是否显示购买提示
        let actives=2;
        if (showType == 1) {
          console.log("确认付款")
          arr = arr.filter(function (obj, i) {
            console.log("picurls："+obj.picUrls)
            return obj.picUrls != undefined;
          })
          console.log(arr)
        }
        if (showType == 2) {
          console.log("待发货")
          actives=3;
          arr = arr.filter(function (obj, i) {
            return obj.purchase == 0;
          })
          console.log(arr)
        }
        // arr.forEach(function (obj) {
        //   obj.checked = false;
        //   totalMoney = totalMoney + obj.orderPrice;
        // });
        // totalNum = arr.length;
        let orderTotal=that.calOrderTotal(arr);
        console.log('*******array*********')
        console.log(orderTotal);
        console.log(arr);
        console.log('*******array*********')
        that.setData({
          orderList: res.data.data,
          lists: arr,
          showTips: showtip,
          active:actives,
          totalMoney: orderTotal.totalMoney,
          totalNum: orderTotal.totalNum
        });
      }
    });
    that.setData({
      showType:showType
    })
  },
  //转采购
  turnBuy:function(e){
      let list=this.data.lists;
      let temp=[];
      let turnList=list.filter(function(obj){
        return obj.checked==true;
      });
    turnList.forEach(function(obj){
        temp.push(obj.id);
    });
    console.log(temp)
    util.request(api.OrderPurchase, { orderIds: temp }, "POST").then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          success: 'success',
          title: '已转采购',
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)

      }
    });
  },
  //切换头部标题
  changeHeaderTitle:function(){
    let index = this.data.showType;
    console.log(index)
    console.log(typeof index)
    // this.getOrderList(index)
    console.log("进入切换头部标题")
    // 标题的切换
    switch (index) {
      case '0':
        wx.setNavigationBarTitle({
          title: '待确认列表'
        });
        break;
      case '1':
        wx.setNavigationBarTitle({
          title: '待收款列表'
        });
        break;
      case '2':
        wx.setNavigationBarTitle({
          title: '待发货列表'
        });
        break;
      case '3':
        wx.setNavigationBarTitle({
          title: '已发货列表'
        });
        break;
      case '4':
        wx.setNavigationBarTitle({
          title: '已收货列表'
        });
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    // filter判断条件符合的数组项组成一个数组返回
    // arr = this.data.orderList.filter(function (obj, i) {
    //   return obj.state == index;
    // })
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderList();
    this.changeHeaderTitle();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})